namespace Maple.Branch.IdentityServer
{
    using Microsoft.Extensions.DependencyInjection;
    using Options;
    using TestDatas;
    using Volo.Abp;
    using Volo.Abp.Autofac;
    using Volo.Abp.Modularity;
    using Volo.Abp.Threading;

    [DependsOn(typeof(IdentityServerDomainModule),
        typeof(AbpTestBaseModule),
        typeof(AbpAutofacModule))]
    public class IdentityServerTestBaseModule : AbpModule
    {
        public override void PreConfigureServices(ServiceConfigurationContext context)
        {
            PreConfigure<BranchIdentityServerBuilderOptions>(options =>
            {
                options.AddDeveloperSigningCredential = false;
            });

            PreConfigure<IIdentityServerBuilder>(identityServerBuilder =>
            {
                identityServerBuilder.AddDeveloperSigningCredential(false, System.Guid.NewGuid().ToString());
            });
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            SeedTestData(context);
        }

        private static void SeedTestData(ApplicationInitializationContext context)
        {
            using var scope = context.ServiceProvider.CreateScope();

            AsyncHelper.RunSync(() => scope.ServiceProvider
                .GetRequiredService<IdentityServerTestDataBuilder>()
                .SeedAsync());
        }
    }
}
